<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addFirstPermission">添加权限</el-button>
      <el-table :data="list" row-key="id">
        <el-table-column size="mini" prop="name" label="名称" />
        <el-table-column size="mini" prop="code" label="标识" />
        <el-table-column size="mini" prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addSecondPermissions(row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="detailsPermission(row.id)">编辑</el-button>
            <el-popconfirm title="确定删除吗？" @onConfirm="confirmDel(row.id)">
              <el-button slot="reference" style="margin-left: 10px" size="mini" type="text">删除</el-button>
            </el-popconfirm>

          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="isDialog"
      width="50%"
      height="500px"
      @close="clickClose"
    >
      <el-form ref="ruleForm" :model="addPermission" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="addPermission.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="addPermission.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="addPermission.description" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch v-model="addPermission.enVisible" active-value="1" inactive-value="0" />
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center" align="middle">
            <el-col :span="13">
              <div style="text-align: center;">
                <el-button type="primary" @click="onSubmit">确认</el-button>
                <el-button @click="clickClose">取消</el-button>
              </div>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
// eslint-disable-next-line no-unused-vars
import { getPermission, addPermission, detailsPermission, editPermission, delPermission } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    const validateName = async(rule, value, callback) => {
      const res = await getPermission()
      console.log(res)
      const flag = res.data.data.some(ele => {
        return ele.name === value
      })
      this.$nextTick(() => {
        console.log(flag)
        if (flag) {
          callback(new Error('权限名称不能重复'))
        } else {
          callback()
        }
      })
    }
    const validateCode = async(rule, value, callback) => {
      const res = await getPermission()
      const flag = res.data.data.some(ele => {
        return ele.code === value
      })
      this.$nextTick(() => {
        console.log(flag)
        if (flag) {
          callback(new Error('权限标识不能重复'))
        } else {
          callback()
        }
      })
    }
    return {
      list: [],
      title: '', // 弹窗的标题
      isDialog: false, // 控制弹窗显示的开关
      isID: '', // 点击节点的ID
      addPermission: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: 0, // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: 0, // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: 0 // 默认关闭
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 1, max: 100, message: '长度在 3 到 10+0 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPermission() // 获取权限点列表
  },
  methods: {
    async getPermission() { // 获取权限点列表
      const res = await getPermission()
      this.list = transListToTreeData(res.data.data, 0)
    },
    addFirstPermission() { // 新增一级权限弹窗
      this.addPermission.pid = 0
      this.addPermission.type = 1
      this.isDialog = true
      this.title = '新增一级权限'
    },
    onSubmit() { // 对整个表单进行校验
      this.$refs.ruleForm.validate(async isOK => {
        if (isOK) {
          if (this.title === '新增一级权限') {
            this.addPermission.type = 1
            const res = await addPermission(this.addPermission)
            this.$message.success(res.data.message) // 新增成功提示
            this.getPermission() // 重新获取数据
            this.clickClose() // 退出并清空表单
          } else if (this.title === '编辑权限') {
            await editPermission(this.addPermission)
            this.$message.success('编辑成功') // 编辑成功提示
            this.getPermission() // 重新获取数据
            this.clickClose() // 退出并清空表单
          } else if (this.title === '新增二级权限') {
            const res = await addPermission(this.addPermission)
            this.$message.success(res.data.message) // 新增成功提示
            this.getPermission() // 重新获取数据
            this.clickClose() // 退出并清空表单
          }
        }
      })
    },
    clickClose() { // 表单清除事件
      this.isDialog = false
      this.$refs.ruleForm.resetFields()
    },
    async detailsPermission(id) { // 编辑权限列表请求
      this.title = '编辑权限'
      const res = await detailsPermission(id)
      console.log(res)
      this.isDialog = true
      this.$nextTick(() => {
        this.isID = id
        this.addPermission = res.data.data // 数据回填
      })
    },
    async confirmDel(id) { // 删除权限列表
      await delPermission(id)
      await this.getPermission() // 重新获取数据
      this.$nextTick(() => {
        this.$message.success('删除成功')
      })
    },
    addSecondPermissions(id) { // 新增二级权限弹窗
      this.addPermission.pid = id
      this.addPermission.type = 2
      this.isDialog = true
      this.title = '新增二级权限'
    }
  }
}
</script>
<style scoped>
.container{
  padding: 20px 0 0 100px;
  background-color: rgb(248, 249, 249);
}
.btn-add {
  margin: 10px;
}
</style>
